package com.sicheng.lc.周赛.分类.dfs记忆化;

import leetcode.editor.cn.tree.TreeNode;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/16 12:02
 */
public class 统计值等于子树平均值的节点数 {
    //    https://leetcode.cn/problems/count-nodes-equal-to-average-of-subtree/
    public int averageOfSubtree(TreeNode root) {
        postOrder(root);
        return res;
    }

    int res = 0;

    public int[] postOrder(TreeNode root) {
        if (root == null)
            return new int[]{0, 0};

        int[] left = postOrder(root.left);
        int[] right = postOrder(root.right);
        left[0] = left[0] + right[0] + root.val;
        left[1] = left[1] + right[1] + 1;
        if (left[0] / left[1] == root.val)
            res++;
        return left;
    }
}
